package com.sunnuwell.demo.service.impl;

import com.sunnuwell.demo.common.exception.AdException;
import com.sunnuwell.demo.common.utils.CommonPageAble;
import com.sunnuwell.demo.common.utils.Utils;
import com.sunnuwell.demo.common.vo.CommonResponse;
import com.sunnuwell.demo.constant.Constants;
import com.sunnuwell.demo.entity.Equipment;
import com.sunnuwell.demo.model.PageModel;
import com.sunnuwell.demo.repository.EquipmentRepository;
import com.sunnuwell.demo.service.EquipmentService;
import com.sunnuwell.demo.vo.EquipmentVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: entry
 * @description: 设备逻辑service
 * @author: Mr.lv
 * @create: 2019-04-24 11:40
 **/
@Service
public class EquipmentServiceImpl implements EquipmentService {

    private EquipmentRepository equipmentRepository;

    private MongoTemplate mongoTemplate;

    public EquipmentServiceImpl(EquipmentRepository equipmentRepository, MongoTemplate mongoTemplate) {
        this.equipmentRepository = equipmentRepository;
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public String save(EquipmentVo equipmentVo) throws AdException, ParseException {
        // 验证参数是否正确
        if (!equipmentVo.validate()) {
//            throw new AdException(Constants.ErrorMsg.REQUEST_PARAM_ERROR);
            throw new AdException(-1,Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }
        // 查看设备名字是否被注册
        if (equipmentVo.getEid() == null) {
            Equipment equipment = equipmentRepository.findByManufacturer(equipmentVo.getManufacturer());
            if (equipment != null) {
//            throw new AdException(Constants.ErrorMsg.SAME_EQUIMENT_ERROR);
                throw new AdException(-1,Constants.ErrorMsg.SAME_EQUIMENT_ERROR);
            }
        }
        equipmentRepository.save(new Equipment(
                equipmentVo.getEid(), equipmentVo.getManufacturer(), equipmentVo.getModel(), equipmentVo.getSn(),
                equipmentVo.getMac(), equipmentVo.getVersion(), equipmentVo.getStatus(), Utils.parseTime2(equipmentVo.getCreatedTime()),
                Utils.parseTime(equipmentVo.getStartTime()), Utils.parseTime(equipmentVo.getEndTime()), equipmentVo.getDescription()
        ));
        return Constants.ErrorMsg.OPERATION_SUCCESS;
    }

    @Override
    public void delete(EquipmentVo equipmentVo) throws AdException {
        if (!equipmentVo.validateEid()) {
            throw new AdException(-1,Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }
        Equipment equipment = new Equipment();
        equipment.setEid(equipmentVo.getEid());
        equipmentRepository.delete(equipment);
    }

    @Override
    public Page<Equipment> findAllEquipment(EquipmentVo equipmentVo) throws AdException {
        // 判断请求参数
        if (!equipmentVo.validateSearch()) {
            throw new AdException(-1,Constants.ErrorMsg.REQUEST_PARAM_ERROR);
        }
        CommonPageAble pageAble = new CommonPageAble();
        PageModel pm = new PageModel();
        Query query = new Query();
        Criteria criteria = new Criteria();
        // 多字段查询
        // 在线状态
        if (!StringUtils.isEmpty(equipmentVo.getStatus())) {
            criteria.and("status").is(equipmentVo.getStatus());
        }
        // 厂商
        if (!StringUtils.isEmpty(equipmentVo.getManufacturer())) {
            criteria.and("manufacturer").is(equipmentVo.getManufacturer());
        }
        // SN
        if (!StringUtils.isEmpty(equipmentVo.getSn())) {
            criteria.and("sn").is(equipmentVo.getSn());
        }
        // MAC
        if (!StringUtils.isEmpty(equipmentVo.getMac())) {
            criteria.and("mac").is(equipmentVo.getMac());
        }
        // 型号
        if (!StringUtils.isEmpty(equipmentVo.getModel())) {
            criteria.and("model").is(equipmentVo.getModel());
        }
        Criteria gt = new Criteria();
        Criteria lt = new Criteria();
        // 活跃时间段
        if (!StringUtils.isEmpty(equipmentVo.getStartTime())) {
            gt.where("startTime").gt(equipmentVo.getStartTime());
        }
        if (!StringUtils.isEmpty(equipmentVo.getEndTime())) {
            lt.where("endTime").lt(equipmentVo.getEndTime());
        }
        if (gt != null && lt != null) {
            criteria.andOperator(gt, lt);
        } else if (gt != null) {
            criteria.andOperator(gt);
        } else if (lt != null) {
            criteria.andOperator(lt);
        }

        List<Sort.Order> orders = new ArrayList<Sort.Order>();  //排序
        // 开始页
        pm.setPagenumber(equipmentVo.getPageNumber());
        // 每页条数
        pm.setPagesize(equipmentVo.getPageSize());
        orders.add(new Sort.Order(Sort.Direction.DESC, "createdTime"));
        Sort sort = new Sort(orders);
        // 排序
        pm.setSort(sort);
        pageAble.setPage(pm);
        query.addCriteria(criteria);
        Long count = mongoTemplate.count(query, Equipment.class);
        // 查询
        List<Equipment> list = mongoTemplate.find(query.with(pageAble), Equipment.class);

        // 将集合与分页结果封装
        Page<Equipment> pagelist = new PageImpl<Equipment>(list, pageAble, count);
        return pagelist;
    }
}